﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ch.Admin.Bfs.Sms.Sdmx.Utils
{
    // CAUTION: Replace this class with InternationalString defined in sdmx Olivier lib in futur


    /// <summary> This class allows to store text in different languages </summary>
    public class InternationalizedString : SerializableDictionary<string, string>
    {
        #region Constructor
        /// <summary> Constructor </summary>
        public InternationalizedString() : this(string.Empty) { }

        /// <summary> Constructor </summary>
        /// <param name="defaultText"> The default text (used for the languages for which no value is defined) </param>
        public InternationalizedString(string defaultText)
        {
            this.DefaultText = defaultText;
        }

        /// <summary> Constructor </summary>
        /// <param name="dictionary"> The mapping between languages and texts </param>
        public InternationalizedString(IDictionary<string, string> dictionary) : this("", dictionary) { }

        /// <summary> Constructor </summary>
        /// <param name="defaultText"> The default text (used for the languages for which no value is defined) </param>
        /// <param name="dictionary"> The mapping between languages and texts </param>
        public InternationalizedString(string defaultText, IDictionary<string, string> dictionary)
            : base(dictionary)
        {
            this.DefaultText = defaultText;
        }
        #endregion

        #region Properties
        /// <summary> The text that is used when no text is defined for a specified language </summary>
        public string DefaultText;

        /// <summary> The text corresponding to the specified language </summary>
        /// <param name="key"> A language </param>
        /// <returns> The text corresponding to the specified language </returns>
        new public string this[string key]
        {
            get
            {
                if (this.ContainsKey(key))
                {
                    return base[key];
                }
                return DefaultText;
            }
            set
            {
                base[key] = value;
            }
        }

        /// <summary> True if and only if neither default text nor text specific to any language is defined </summary>
        public bool IsEmpty
        {
            get
            {
                return (this.Count < 1) && string.IsNullOrEmpty(this.DefaultText);
            }
        }

        /// <summary> Return the lenght of the longest string in the international string </summary>
        public int MaxLength
        {
            get
            {
                int result = this.DefaultText != null ? this.DefaultText.Length : 0;
                foreach (string value in this.Values)
                {
                    result = Math.Max(result, value.Length);
                }

                return result;
            }
        }
        #endregion

        #region Equals, GetHashCode, ToString Methods
        public override int GetHashCode()
        {
            int prime = 31;
            int result = this.DefaultText.GetHashCode();
            foreach (string key in this.Keys)
            {
                result = result * prime + key.GetHashCode() + this[key].GetHashCode();
            }

            return result;
        }

        public override bool Equals(Object obj)
        {
            InternationalizedString other = obj as InternationalizedString;

            bool result = (this.DefaultText == other.DefaultText) && (this.Keys.Count == other.Keys.Count);
            if (result)
            {
                foreach (string key in this.Keys)
                {
                    result = result && other.ContainsKey(key) && this[key] == other[key];
                }
            }
            return result;
        }
        public override string ToString()
        {
            StringBuilder result = new StringBuilder();
            if (this.DefaultText != null)
            {
                result.Append("default='").Append(this.DefaultText).Append("';");
            }
            foreach (string language in this.Keys)
            {
                result.Append(language).Append("='").Append(this[language]).Append("';");
            }
            return result.ToString();
        }
        #endregion
    }
}
